/*
 * simpan_kirim.java
 *
 * Created on 20 Juni 2008, 0:16
 */

package sms_rac_v3;
import javax.swing.table.DefaultTableModel;
import java.awt.Window;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.*;




/**
 *
 * @author  Rachmad
 */
public class simpan_kirim extends javax.swing.JFrame {
    
    /** Creates new form simpan_kirim */
    public simpan_kirim() {
        initComponents();
        koneksiDB.sambungDB(); // menghubungkan dengan data base
        tampil(); //menampilakan semua pesan yang diterima
        apl.prosesMulai(); // memulai konfigure
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txTujuan = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        taPesan = new javax.swing.JTextArea();
        btKirim = new javax.swing.JButton();
        btBersih = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tbPesan = new javax.swing.JTable();
        jPanel3 = new javax.swing.JPanel();
        proses = new java.awt.List();

        getContentPane().setLayout(null);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        jPanel1.setLayout(null);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Kirim SMS", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(255, 153, 0)));
        jLabel1.setText("Nomer Tujuan");
        jPanel1.add(jLabel1);
        jLabel1.setBounds(20, 30, 80, 14);

        jPanel1.add(txTujuan);
        txTujuan.setBounds(100, 30, 170, 20);

        jLabel2.setText("Pesan");
        jPanel1.add(jLabel2);
        jLabel2.setBounds(20, 60, 70, 14);

        jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        taPesan.setColumns(20);
        taPesan.setRows(5);
        taPesan.setAutoscrolls(false);
        jScrollPane1.setViewportView(taPesan);

        jPanel1.add(jScrollPane1);
        jScrollPane1.setBounds(100, 60, 170, 160);

        btKirim.setText("Kirim");
        btKirim.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btKirimActionPerformed(evt);
            }
        });

        jPanel1.add(btKirim);
        btKirim.setBounds(10, 240, 100, 23);

        btBersih.setText("Bersih");
        btBersih.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btBersihActionPerformed(evt);
            }
        });

        jPanel1.add(btBersih);
        btBersih.setBounds(170, 240, 100, 23);

        getContentPane().add(jPanel1);
        jPanel1.setBounds(10, 10, 290, 280);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Pesan Masuk", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(255, 153, 0)));
        jScrollPane2.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        tbPesan.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tbPesan.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbPesanMouseClicked(evt);
            }
        });

        jScrollPane2.setViewportView(tbPesan);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 431, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        getContentPane().add(jPanel2);
        jPanel2.setBounds(310, 10, 450, 180);

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Proses", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(255, 153, 0)));

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(proses, javax.swing.GroupLayout.DEFAULT_SIZE, 434, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(proses, javax.swing.GroupLayout.PREFERRED_SIZE, 69, Short.MAX_VALUE)
                .addGap(1, 1, 1))
        );
        getContentPane().add(jPanel3);
        jPanel3.setBounds(310, 190, 450, 0);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-775)/2, (screenSize.height-332)/2, 775, 332);
    }// </editor-fold>//GEN-END:initComponents

    private void tbPesanMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbPesanMouseClicked
// TODO add your handling code here:
       try {
            int n = tbPesan.getSelectedRow();
        txTujuan.setText(tbPesan.getValueAt(n,1).toString() );
        if(tbPesan.getValueAt(n,2).toString().length()>20){
        taPesan.setText(tbPesan.getValueAt(n,2).toString() );
        }
        } catch (Exception e) {
            System.out.println("Ini kesalah klik:"+e);
        } 
    }//GEN-LAST:event_tbPesanMouseClicked

    private void btBersihActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btBersihActionPerformed
// TODO add your handling code here:
        txTujuan.setText("");
        taPesan.setText("");
    }//GEN-LAST:event_btBersihActionPerformed
    
    private void btKirimActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btKirimActionPerformed
// TODO add your handling code here:
      apl.KirimSms(txTujuan.getText().trim().toString(),taPesan.getText().trim().toString());
        
    }//GEN-LAST:event_btKirimActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new simpan_kirim().setVisible(true);
            }
        });
    }
    
     public static void tampil(){
       // System.out.println("dijalankan");
        //int jml = koneksiDB.hitung_data("select * from kotak_masuk");
        try{
            if(tbPesan.getRowCount()>0){
               // System.out.println("Jumlah row :"+pesan.getRowCount());
                 tbPesan.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "No", "Pengirim",  "Pesan"}
        ));
            }
            stmn= koneksiDB.con.createStatement();
            rs = stmn.executeQuery(sql_query);
            model = (DefaultTableModel)tbPesan.getModel();
            //tbAlat.setModel(model);
           
            //-----------
            while (rs.next()){
                for (int i=0;i<3;i++){
                    data[i]=rs.getString(i+1);
                }
              model.addRow(data);
             // System.out.println("isi data ");
            }
           // tbKonsumen.setEnabled(false);
            //tbKonsumen.setRowSelectionInterval(0,0); untuk sementara
            
        }catch (Exception e){
            System.out.println("ada masalah tampilkan kotak_masuk: "+ e);
        }
        
    }
    
  
    //Metod untuk pengisian pulsa
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btBersih;
    private javax.swing.JButton btKirim;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    public static java.awt.List proses;
    private javax.swing.JTextArea taPesan;
    public static javax.swing.JTable tbPesan;
    private javax.swing.JTextField txTujuan;
    // End of variables declaration//GEN-END:variables
    proses_sms apl = new proses_sms();
    static DefaultTableModel model;
    static Object data[] = new Object[10];
    static Statement stmn,stmn2;
    static ResultSet rs,rs2;
    static String sql_query="select kode as no, pengirim , pesan from kotak_masuk" +
            " where sts = '0' ;";
}
